package com.tgky.ybzb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tgky.ybzb.common.PageParam;
import com.tgky.ybzb.entity.Jcyb;
import com.tgky.ybzb.mapper.JcybMapper;
import com.tgky.ybzb.utils.MPUtil;
import com.tgky.ybzb.entity.Jcybj;
import com.tgky.ybzb.mapper.JcybjMapper;
import com.tgky.ybzb.service.IJcybjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 决策样本集 服务实现类
 *
 * @author XYP
 * @since 2024-07-22
 */
@Service
public class JcybjServiceImpl extends ServiceImpl<JcybjMapper, Jcybj> implements IJcybjService {
    @Resource
    private JcybjMapper jcybjMapper;
    @Resource
    private JcybServiceImpl jcybService;
    @Resource
    private JcybMapper jcybMapper;

    /**
     * 通过实体类获取QueryWrapper
     * @param pageParam 分页参数
     * @param jcybj 实体信息
     * @return
     */
    @Override
    public QueryWrapper<Jcybj> getQueryWrapper(PageParam pageParam, Jcybj jcybj){
        QueryWrapper<Jcybj> queryWrapper = MPUtil.getQueryWrapper(pageParam);
        queryWrapper.orderByDesc("create_time");
        if(jcybj == null){
            return queryWrapper;
        }

        //主键
        if(jcybj.getId() != null){
            queryWrapper.lambda()
                    .eq(Jcybj::getId, jcybj.getId());
            return queryWrapper;
        }

        //TODO 此处可以根据各字段查询需求修改查询条件，eq、like、ge、gt、le、lt、ne...等等等
        queryWrapper.lambda()
                .eq(jcybj.getId() != null, Jcybj::getId, jcybj.getId())
                .like(jcybj.getXqcj() != null, Jcybj::getXqcj, jcybj.getXqcj())
                .like(jcybj.getMbssdlqy() != null, Jcybj::getMbssdlqy, jcybj.getMbssdlqy())
                .eq(jcybj.getYbsl() != null, Jcybj::getYbsl, jcybj.getYbsl());
        return queryWrapper;
    }

    /**
     * 获取决策样本集和决策样本数据
     * @param pageParam
     * @param jcybj
     * @return
     */
    @Override
    public IPage<Jcybj> getJcybjListAndJcyb(Page pageParam, Jcybj jcybj) {
        IPage<Jcybj> jcybjPage=jcybjMapper.selectPage(pageParam,getQueryWrapper(null,jcybj));
        jcybjPage.getRecords().stream().forEach(obj -> {
            if(jcybj.getJcyb()==null){
                jcybj.setJcyb(new Jcyb());
            }
            jcybj.getJcyb().setJcybjId(obj.getId());
            List<Jcyb> jcybList=jcybMapper.selectList(jcybService.getQueryWrapper(null,jcybj.getJcyb()));
            obj.setJcybList(jcybList);
        });
        return jcybjPage;
    }
}
